# Pipit 构建系统

# 支持在类Unix系统上构建和运行Pipit项目
# Windows用户需要安装WSL或Cygwin环境

# 颜色输出
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
NC = \033[0m # 无颜色

# 变量定义
CC ?= gcc
CFLAGS ?= -O2 -Istdlib/c/include
RUST_TARGET ?= release
CORE_DIR = core
EXAMPLES_DIR = examples
STDLIB_DIR = stdlib/c
STDLIB_LIB_DIR = $(STDLIB_DIR)/lib
STDLIB_SRC_DIR = $(STDLIB_DIR)/src

# 默认目标
.PHONY: all clean core run run-example run-concurrent run-plugin run-comprehensive run-variables run-enhanced test format check download-deps build-c-stdlib

# 检查依赖
check-deps:
	@echo "$(GREEN)[INFO]$(NC) 检查依赖..."
	@which rustc > /dev/null || (echo "$(RED)[ERROR]$(NC) Rust编译器未找到，请先安装Rust" && exit 1)
	@which cargo > /dev/null || (echo "$(RED)[ERROR]$(NC) Cargo未找到，请先安装Rust" && exit 1)
	@which $(CC) > /dev/null || (echo "$(RED)[ERROR]$(NC) C编译器 ($(CC)) 未找到，请安装C编译器" && exit 1)
	@echo "$(GREEN)[INFO]$(NC) 所有依赖检查成功"

# 下载依赖
download-deps:
	@echo "$(GREEN)[INFO]$(NC) 下载依赖..."
	@which cargo > /dev/null || (echo "$(RED)[ERROR]$(NC) Cargo未找到。请先安装Rust" && exit 1)
	@echo "$(GREEN)[INFO]$(NC) 下载Rust依赖..."
	@cd $(CORE_DIR) && cargo fetch
	@echo "$(GREEN)[INFO]$(NC) 依赖下载成功"

# 构建C标准库
build-c-stdlib:
	@echo "$(GREEN)[INFO]$(NC) 构建C标准库..."
	@mkdir -p $(STDLIB_LIB_DIR)
	@for file in $(STDLIB_SRC_DIR)/*.c; do \
		if [ -f "$$file" ]; then \
			echo "$(GREEN)[INFO]$(NC) 编译 $$(basename $$file)..."; \
			$(CC) -c $$file $(CFLAGS) -o $(STDLIB_LIB_DIR)/$$(basename $$file .c).o || exit 1; \
		fi \
	done
	@cd $(STDLIB_LIB_DIR) && ar rcs libpipit_stdlib.a *.o 2>/dev/null || echo "$(YELLOW)[WARNING]$(NC) 没有目标文件可创建静态库"
	@echo "$(GREEN)[INFO]$(NC) C标准库构建成功"

all: check-deps build-c-stdlib core

# 构建核心 (Rust)
.PHONY: core
core:
	cd $(CORE_DIR) && cargo build --release

# 构建所有
.PHONY: build
build: core

# 快速构建（跳过优化）
.PHONY: quick
quick:
	cd $(CORE_DIR) && cargo build

# 清理构建产物
.PHONY: clean
clean:
	cd $(CORE_DIR) && cargo clean
	rm -f $(STDLIB_LIB_DIR)/*.o
	rm -f $(STDLIB_LIB_DIR)/*.a
	@echo "$(GREEN)[INFO]$(NC) 清理完成"

# 清理示例文件（旧的.sl文件）
.PHONY: clean-examples
clean-examples:
	rm -f $(EXAMPLES_DIR)/*.sl

# 安装依赖
.PHONY: install-deps
install-deps:
	# 安装 C 编译器（MinGW或Visual C++ Build Tools）
	@echo "请确保安装了C编译器（如MinGW或Visual C++ Build Tools）"

# 运行默认示例
.PHONY: run
run: build
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行示例，因为项目结构中不存在运行时模块"

# 运行示例程序
run-example:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行示例，因为项目结构中不存在运行时模块"

run-concurrent:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行并发示例，因为项目结构中不存在运行时模块"

run-plugin:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行插件示例，因为项目结构中不存在运行时模块"

run-comprehensive:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行综合示例，因为项目结构中不存在运行时模块"

run-variables:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行变量示例，因为项目结构中不存在运行时模块"

run-enhanced:
	@echo "$(YELLOW)[WARNING]$(NC) 无法运行增强示例，因为项目结构中不存在运行时模块"

# 测试
.PHONY: test
test: core
	cd $(CORE_DIR) && cargo test

# 格式化代码
.PHONY: format
format:
	cd $(CORE_DIR) && cargo fmt

# 检查代码
.PHONY: check
check:
	cd $(CORE_DIR) && cargo check

# 帮助信息
.PHONY: help
help:
	@echo "Pipit 构建选项:"
	@echo "  all                 - 构建核心和C标准库 (默认)"
	@echo "  core                - 仅构建核心"
	@echo "  build-c-stdlib      - 仅构建C标准库"
	@echo "  build               - 构建所有组件"
	@echo "  quick               - 快速构建（跳过优化）"
	@echo "  clean               - 清理构建产物"
	@echo "  clean-examples      - 清理旧的 .sl 示例文件"
	@echo "  download-deps       - 下载依赖"
	@echo "  test                - 运行测试"
	@echo "  format              - 格式化代码"
	@echo "  check               - 检查代码"
	@echo "  help                - 显示此帮助信息"